# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import decimal
Dec = decimal.Decimal

import Utils
import ShareProfile
import Category

class Bill:
	def __init__(self, value, date, paidBy, sharedBy, details=None, comments=None):
		self.ID = None
		#the total cost of the bill
		self.setValue(value)
		#the datetime.date() instance representing the date the bill is from
		self.setDate(date)
		#the string name of the person that paid the bill
		self.setPaidBy(paidBy)
		#a list of names of the people that consumed the bill,
		#or a category that is belongs in
		self.setSharedBy(sharedBy)
		#details on this purchase (optional)
		self.setDetails(details)
		self.setComments(comments)
		
	def __cmp__(self, object):
		for i in ("date", "value", "paidBy", "details", "comments", "category", "sharedBy", "ID"):
			if hasattr(object, i):
				val = cmp(getattr(self, i), getattr(object, i))
				if val != 0:
					return val
			else:
				return -1
		return 0
		
	def copy(self):
		b = Bill(self.value, self.date, self.paidBy, None, self.details, self.comments)
		if self.sharedBy:
			b.sharedBy = self.sharedBy.copy()
		else:
			b.category = self.category
		b.ID = self.ID
		return b
	
	def setValue(self, value):
		self.value = Dec(value)
		
	def setDate(self, date):
		if type(date) == str:
			self.date = Utils.dateFromStr(date)
		else:
			self.date = date
	
	def setPaidBy(self, paidBy):
		self.paidBy = paidBy
		
	def setSharedBy(self, sharedBy):
		if isinstance(sharedBy, Category.Category):
			self.category = sharedBy
			self.sharedBy = None
		elif isinstance(sharedBy, ShareProfile.ShareProfile):
			self.sharedBy = sharedBy
			self.category = None
		else:
			self.sharedBy = None
			self.category = None
	
	def setDetails(self, details):
		self.details = details or ""
		
	def setComments(self, comments):
		self.comments = comments or ""
	
	def getValue(self):
		return self.value
	
	def getDate(self):
		return self.date
	
	def getPaidBy(self):
		if self.category and self.category.getPaidBy():
			return self.category.getPaidBy()
		else:
			return self.paidBy
	
	def getSharedBy(self):
		if self.category:
			return self.category.getSharedBy()
		else:
			return self.sharedBy
	
	def isSharedBy(self, name):
		return name in self.getSharedBy()
		
	def getCategory(self):
		return self.category
	
	def getDetails(self):
		return self.details
	
	def getComments(self):
		return self.comments
	
	def swapPerson(self, person, newPerson):
		if self.paidBy == person:
			self.paidBy = newPerson
		if self.sharedBy:
			self.sharedBy.swapPerson(person, newPerson)
		# we don't have to swap people in our category, because account will change the categories
		
	def swapCategory(self, category, newCategory):
		if self.category == category:
			self.category = newCategory
